package com.yili.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yili.common.result.Result;
import com.yili.model.entity.UserAddress;
import com.yili.userservice.mapper.UserAddressMapper;
import com.yili.userservice.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public List<UserAddress> getUserAddresses(Long userId) {
        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAddress::getUserId, userId)
               .eq(UserAddress::getIsDeleted, false)
               .orderByDesc(UserAddress::getIsDefault)
               .orderByDesc(UserAddress::getUpdateTime);
        return userAddressMapper.selectList(wrapper);
    }

    @Override
    public UserAddress addUserAddress(UserAddress address) {
        address.setCreateTime(System.currentTimeMillis());
        address.setUpdateTime(System.currentTimeMillis());
        address.setIsDeleted(false);
        
        // 如果是第一个地址，设为默认地址
        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAddress::getUserId, address.getUserId())
               .eq(UserAddress::getIsDeleted, false);
        int count = userAddressMapper.selectCount(wrapper);
        address.setIsDefault(count == 0);

        userAddressMapper.insert(address);
        return address;
    }

    @Override
    public UserAddress updateUserAddress(Long id, UserAddress address) {
        UserAddress existingAddress = userAddressMapper.selectById(id);
        if (existingAddress == null || existingAddress.getIsDeleted()) {
            throw new RuntimeException("地址不存在");
        }

        address.setId(id);
        address.setUpdateTime(System.currentTimeMillis());
        address.setIsDeleted(false);
        address.setUserId(existingAddress.getUserId()); // 防止修改用户ID
        address.setIsDefault(existingAddress.getIsDefault()); // 保持默认状态不变

        userAddressMapper.updateById(address);
        return address;
    }

    @Override
    public void deleteUserAddress(Long id) {
        UserAddress address = userAddressMapper.selectById(id);
        if (address == null || address.getIsDeleted()) {
            throw new RuntimeException("地址不存在");
        }

        // 逻辑删除
        address.setIsDeleted(true);
        address.setUpdateTime(System.currentTimeMillis());
        userAddressMapper.updateById(address);
    }

    @Override
    @Transactional
    public void setDefaultAddress(Long userId, Long addressId) {
        // 先将该用户的所有地址设为非默认
        LambdaUpdateWrapper<UserAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserAddress::getUserId, userId)
               .eq(UserAddress::getIsDeleted, false)
               .set(UserAddress::getIsDefault, false);
        userAddressMapper.update(null, wrapper);

        // 将指定地址设为默认
        UserAddress address = userAddressMapper.selectById(addressId);
        if (address == null || address.getIsDeleted()) {
            throw new RuntimeException("地址不存在");
        }
        if (!userId.equals(address.getUserId())) {
            throw new RuntimeException("无权操作此地址");
        }

        address.setIsDefault(true);
        address.setUpdateTime(System.currentTimeMillis());
        userAddressMapper.updateById(address);
    }
} 